package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.Meeting;
import com.zch.dao.model.MeetingExample;
import com.zch.daoEx.model.MeetingEx;
import com.zch.service.MeetingService;

@RequestMapping("meeting")
@Controller
@LoginFilter
public class MeetingController extends CoreController{

	@Autowired
	private MeetingService meetingService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<MeetingEx> list = meetingService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}
	
	@RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        MeetingExample example = new MeetingExample();
        if(name == null){
            name = "";
        }
        example.createCriteria().andTitleLike("%"+name+"%").andUserIdEqualTo(id);
        example.setOrderByClause("create_time desc");
        List<Meeting> list = meetingService.selectByExample(example);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }

	@RequestMapping("/save")
	@ResponseBody
	public String save(Meeting meeting){
		if (StringUtils.isBlank(meeting.getId())){
			meetingService.insert(meeting);
		}else{
			meetingService.updateByPrimaryKeySelective(meeting);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Meeting meeting = meetingService.selectByPrimaryKey(id);
		meeting.setContent(meeting.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
		return JSONUtils.serialize(meeting);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			meetingService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Meeting meeting){
	    meeting.setUserId(getSessionParameter("id"));
	    if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(meeting.getId())){
            meetingService.insert(meeting);
        }else{
            meetingService.updateByPrimaryKeySelective(meeting);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}